//
//  BirthdayViewController.m
//  Light
//
//  Created by 骁唐 on 15/9/7.
//  Copyright (c) 2015年 骁唐. All rights reserved.
//

#import "BirthdayViewController.h"
#import "UserConfig.h"

@interface BirthdayViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *backGround;
@property (weak, nonatomic) IBOutlet UIPickerView *myPickerView;
@property (strong,nonatomic) NSMutableArray *yearArray;
@property (strong,nonatomic) NSMutableArray *monthArray;
//当前设置的年月
@property (copy,nonatomic) NSString *year;
@property (copy,nonatomic ) NSString *month;
@end

@implementation BirthdayViewController
{
    NSString *  currentYear;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.year = @"1980";
    self.month = @"1";
    
    NSDate *  senddate=[NSDate date];
    
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    
    [dateformatter setDateFormat:@"YYYY"];
    
    currentYear=[dateformatter stringFromDate:senddate];
    
    NSLog(@"locationString:%@",currentYear);
    
    int yearMax = [currentYear intValue];
    
    self.yearArray = [[NSMutableArray alloc]init];
    for  (int i = 1960; i<= yearMax; i++) {
        [self.yearArray addObject:[NSString stringWithFormat:@"%d",i]];
    }
    self.monthArray = [[NSMutableArray alloc] init];
    for (int i = 1; i <= 12; i++) {
        [self.monthArray addObject:[NSString stringWithFormat:@"%d",i]];
    }
    
    //设置默认在compoent列中默认选中row行
    [self.myPickerView selectRow:20 inComponent:0 animated:YES];
}

- (void)viewDidLayoutSubviews {
    ((UIView *)[_myPickerView.subviews objectAtIndex:1]).backgroundColor = [UIColor clearColor];
    ((UIView *)[_myPickerView.subviews objectAtIndex:2]).backgroundColor = [UIColor clearColor];
}
-(NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView{
   // NSLog(@"调用%s ,%d",__FUNCTION__,__LINE__);
    
    //返回有几列 ，注意
    return 4;
    
}

//返回指定列的行数
- (NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
   //NSLog(@"调用%s ,%d",__FUNCTION__,__LINE__);
    if (component==0) {
        return  [self.yearArray count];
    } else if(component==1){
        
        return  1;
    }
    else if(component==2){
        return [self.monthArray count];
        
    }else if(component==3){
        return 1;
        
    }

    
    return 0;
}

//以下是代理部分，可以自定义视图

//返回指定列，行的高度，就是自定义行的高度
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    
  //  NSLog(@"调用%s ,%d",__FUNCTION__,__LINE__);
    
    
    return  54;
}
//返回指定列的宽度
- (CGFloat) pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
  //  NSLog(@"调用%s ,%d",__FUNCTION__,__LINE__);
    if (component==0) {
        //第0列，宽
        return  80  ;
    } else if(component==1){
        //第1列，宽
        return  40;
    }
    else if(component==2){
        //第2列宽
        return 50;
    }else {
        //第三列宽
        return 40;
    }

}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    
   // NSLog(@"调用%s ,%d",__FUNCTION__,__LINE__);
    
    
    //思想就是：先创建一个View以指定列的宽度，和所在列行的高度，为尺寸
    //再建立一个label,在这个view上显示字体，字体颜色，字体大小，然后，把这个label添加到view中
    //返回view，作为指定列的每行的视图
    
    //取得指定列的宽度
    CGFloat width=[self pickerView:pickerView widthForComponent:component];
    
    //取得指定列，行的高度
    CGFloat height=[self pickerView:pickerView rowHeightForComponent:component];
    //定义一个视图
    UIView *myView=[[UIView alloc] init];
    
    //指定视图frame
    myView.frame=CGRectMake(0, 0, width, height);
    
    UILabel *labelOnComponent=[[UILabel alloc] init];
    
    labelOnComponent.frame=myView.frame;
//    labelOnComponent.tag=200;
    
    if (component==0) {
        //如果是第0列
        labelOnComponent.text= [self.yearArray objectAtIndex:row];
        labelOnComponent.textColor = [UIColor whiteColor];
        labelOnComponent.textAlignment = UIBaselineAdjustmentAlignCenters;
        [labelOnComponent setNumberOfLines:0];
        labelOnComponent.font = [UIFont fontWithName:@"Helvetica" size:35];
        labelOnComponent.adjustsFontSizeToFitWidth = YES;
    }
    else if(component==1){
        //如果是第1列
        //以说选择行为索引，取得颜色数组中的颜色，并把label的背景色设为该颜色
        labelOnComponent.text = @"年";
        labelOnComponent.textColor = [UIColor whiteColor];
        labelOnComponent.textAlignment = UIBaselineAdjustmentAlignCenters;
        [labelOnComponent setNumberOfLines:0];
        labelOnComponent.font = [UIFont fontWithName:@"Helvetica" size:20];
        labelOnComponent.adjustsFontSizeToFitWidth = YES;
    }
    else if(component==2){
        //如果是第2列
        //label 上显示的是相应字体
        labelOnComponent.text= [self.monthArray objectAtIndex:row];
        labelOnComponent.textColor = [UIColor whiteColor];
        labelOnComponent.textAlignment = UIBaselineAdjustmentAlignCenters;
        [labelOnComponent setNumberOfLines:0];
        labelOnComponent.font = [UIFont fontWithName:@"Helvetica" size:35];
        labelOnComponent.adjustsFontSizeToFitWidth = YES;
    }
    else if(component==3){
        labelOnComponent.text = @"月";
        labelOnComponent.textColor = [UIColor whiteColor];
        labelOnComponent.textAlignment = UIBaselineAdjustmentAlignCenters;
        [labelOnComponent setNumberOfLines:0];
        labelOnComponent.font = [UIFont fontWithName:@"Helvetica" size:20];
        labelOnComponent.adjustsFontSizeToFitWidth = YES;
    }
    
    [myView addSubview:labelOnComponent];
    return myView;
}

- (void)pickerView:(UIPickerView *)pickerView
      didSelectRow:(NSInteger)row
       inComponent:(NSInteger)component
{
    if (component == 0) {
        //获取当前时间
        //setHour 设置的时间
        self.year = self.yearArray[row];
    }
    else if (component == 2) {
        self.month = self.monthArray[row];
    }
}

#pragma mark -
#pragma mark 导航返回按钮
- (IBAction)backClick {
    [self.navigationController popViewControllerAnimated:YES];
 
}

#pragma mark -
#pragma mark 点击进入下一个界面
- (IBAction)nextClick {
    UserConfig *userConfig = [UserConfig sharingUserConfig];
    //记录生日
    [userConfig setCurrentBirthday:[NSString stringWithFormat:@"%@-%@-01",self.year,self.month]];
    
    int age = [currentYear intValue] - [self.year intValue];
    //记录年龄
    [userConfig setCurrentAge:[NSString stringWithFormat:@"%d",age]];
    
    [self performSegueWithIdentifier:@"birthdayToHeight" sender:nil];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //强制竖屏
    [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:
                                        UIInterfaceOrientationPortrait]  forKey:@"orientation"];
    //SXW__标记，设置背景
    long long int currentTimeInt = [TJJDefines getTimeIntSince1970Current];
    if (currentTimeInt>=16200&& currentTimeInt <=34200) {
        self.backGround.image = [UIImage imageNamed:@"backgroundOne"];
    }else if (currentTimeInt>34200 && currentTimeInt<=55800)
    {
        self.backGround.image = [UIImage imageNamed:@"backgroundTwo"];
    }else if (currentTimeInt>55800 && currentTimeInt<=70200)
    {
        self.backGround.image = [UIImage imageNamed:@"backgroundThree"];
    }else if(currentTimeInt >70200 || currentTimeInt<16200)
    {
        self.backGround.image = [UIImage imageNamed:@"background_night"];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */
@end
